import { getPortalUserInfo } from "@/api/user.js"
// #ifdef APP-PLUS
import { pathByUrl,downToPath } from "@/services/mediaService"
// #endif

export class User {
	
	userId
	
	userNum
	
	userName
	
	avatarUrl
	
	saveAvatar
	
	remarks
	
	profiles 
	
	mobile 
	
	sex
	
	buddy = false 
	
	province 
	
	city
	
	async fetchOrigin(){
		const info = await getPortalUserInfo(this.userId)
		const ins = User.origin2Ins(info)
		this.mixIns(ins)
		// #ifdef APP-PLUS
		pathByUrl(this.avatarUrl).then((path)=>{
			if(path){
				this.saveAvatar = path
			}else{
				downToPath(this.avatarUrl).then((newPath)=>{
					this.saveAvatar = newPath
				})
			}
		})
		// #endif
		return this
	}
	
	mixIns(ins){
		
		const {userId,...props} = ins
		Object.assign(this,props)
		return this
	}
	
	static creatNullIns({userId,userName,avatarUrl}){
		if(!userId){
			throw new Error('userId不能为空')
		}
		const ins = new User()
		ins.userId = userId
		ins.avatarUrl = avatarUrl
		ins.userName = userName
		return ins
	}
	
	static origin2Ins(info){
		const ins = new User()
		ins.userId = info.userId
		ins.userName = info.userName
		ins.avatarUrl = info.avatarUrl
		
		ins.userNum = info.userNum
		ins.remarks = info.remarks
		ins.profiles = info.profiles
		ins.mobile = info.mobile
		ins.sex = info.sex
		ins.buddy = !!info.buddy
		ins.province = info.province
		ins.city = info.city
		
		return ins
	}
	
}